package control;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import model.LyricsPage;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.br.BrazilianAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class Main {
	private static String DIR_INDEX_PATH = "indexDir";
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		URL myUrl;
		String url = "http://letras.terra.com.br/plugin.php";
		
		
		try {
			myUrl = new URL(url);

			Crawler crw = new Crawler(0, 1000, myUrl.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
//		searchString("don't cry");
		
	}

	public static void searchString(String queryString) {
		Analyzer analyzer = new BrazilianAnalyzer(Version.LUCENE_33);
		try {
			
			File idxDirPath = new File(DIR_INDEX_PATH);
			FSDirectory indexDir = null;
			indexDir = FSDirectory.open(idxDirPath);
			
			//createIndexWriter(filesDir, analyzer, indexDir);
			
			IndexSearcher idxSearcher = new IndexSearcher(indexDir);
			QueryParser parser = new QueryParser(Version.LUCENE_33, IndexFields.LETRA.getKey(), analyzer);
			
			Query query;
			try {
				query = parser.parse(queryString);
				TopDocs results = idxSearcher.search(query, 20);
				
				for(ScoreDoc scrDoc : results.scoreDocs){
					Document doc = idxSearcher.doc(scrDoc.doc);
					System.out.println(doc.get("singer"));
					System.out.println(doc.get("title"));
					System.out.println(doc.get("lyrics"));
					System.out.println(doc.get("url"));
					System.out.println("=====================");
				}
				
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			
			
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

}


